package com.cameron.serviceedu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cameron.serviceedu.entity.EduCourse;
import com.cameron.entity.EduTeacher;
import com.cameron.serviceedu.service.EduCourseService;
import com.cameron.serviceedu.service.EduTeacherService;
import com.cameron.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/service/teacher-front")
@Slf4j
@SuppressWarnings("unchecked")
public class TeacherController {

    @Resource
    private EduTeacherService teacherService;

    @Resource
    private EduCourseService courseService;

    @GetMapping("getTeacherList/{currentPage}/{pageSize}")
    public Result getTeacherList(@PathVariable int currentPage, @PathVariable int pageSize){
        Page<EduTeacher> page = new Page<>(currentPage,pageSize);

        LambdaQueryWrapper<EduTeacher> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(EduTeacher::getLevel);
        teacherService.page(page,lqw);

        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();

        map.put("items",records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return Result.ok().data(map);
    }

    @GetMapping("getTeacherInfo/{teacherId}")
    public Result getTeacherInfo(@PathVariable String teacherId) {
        //查询讲师基本信息
        EduTeacher teacher = teacherService.getById(teacherId);

        //根据讲师id查询课程
        LambdaQueryWrapper<EduCourse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EduCourse::getTeacherId,teacherId);
        List<EduCourse> list = courseService.list(lqw);

        return Result.ok().data("teacher",teacher).data("courseList",list);
    }
}
